package com.gkoudai.service;

import com.gkoudai.open.platform.client.HttpRequestClient;
import com.gkoudai.open.platform.common.CategoryCommon;
import com.gkoudai.open.platform.common.ExchangeCodeEnum;
import com.gkoudai.open.platform.common.HotListCategorizeEnum;
import com.gkoudai.open.platform.common.KlineCycleEnum;
import com.gkoudai.open.platform.entry.IndicatorsDetailDataRequestBody;
import com.gkoudai.open.platform.entry.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.annotation.Schedules;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author wangqi
 * @date 2024-11-06 18:12
 */
@Component
public class HttpService {
    private final HttpRequestClient httpRequestClient;

    @Autowired
    public HttpService(HttpRequestClient httpRequestClient) {
        this.httpRequestClient = httpRequestClient;
    }

    /**
     * 龙虎榜
     */
    public void futureDragonLongShortRatio() {
        //龙虎榜品种列表
        ResponseBody<List<QuoteConfig>> listResponseBody = httpRequestClient.requestFutureDragonQuoteList(1, 50);
        //多空比
        ResponseBody<MultipleSpaceRatio> multipleSpaceRatioResponseBody = httpRequestClient.requestFutureDragonLongShortRatio(LocalDate.now());
        List<PositionRatio> positionRatioList = multipleSpaceRatioResponseBody.getData().getPositionRatioList();
        PositionRatio positionRatio = positionRatioList.get(0);
        String showCode = positionRatio.getShowCode();
        //多空持仓
        ResponseBody<BuySellPosition> buySellPositionResponseBody = httpRequestClient.requestFutureDragonLongShortPosition(showCode);
        //净持仓
        ResponseBody<NetPosition> buySellPositionResponseBody1 = httpRequestClient.requestFutureDragonNetPosition(showCode);
        System.out.println();
    }

    /**
     * 异动持仓
     */
    public void abnormalPosition() {
        ResponseBody<PositionPageResponse> positionPageResponseResponseBody = httpRequestClient.requestAbnormalPosition(LocalDate.now(), 1, 20);
        System.out.println();
    }

    /**
     * 热门榜单
     */
    public void hotList() {
        //成交额榜单
        ResponseBody<DataList<Hot>> turnoverListResponseBody = httpRequestClient.requestHotList(HotListCategorizeEnum.TURNOVER);
        //跌幅榜
        ResponseBody<DataList<Hot>> declineListResponseBody = httpRequestClient.requestHotList(HotListCategorizeEnum.DECLINE);
        //涨幅榜
        ResponseBody<DataList<Hot>> riseListResponseBody = httpRequestClient.requestHotList(HotListCategorizeEnum.RISE);
        System.out.println();
    }

    /**
     * 品种推荐
     */
    public void breedCycleRecommendation() {
        ResponseBody<DataList<BreedCycleRecommendation>> dataListResponseBody = httpRequestClient.requestBreedCycleRecommendation(10);
        System.out.println();
    }

    /**
     * 农产品指标
     */
    public void agriculturalProductsInfo() {
        ResponseBody<List<MetricInformation>> listResponseBody = httpRequestClient.requestAgriculturalProductsInfo(CategoryCommon.AgriculturalCategoryEnum.ALL);
        MetricInformation metricInformation1 = listResponseBody.getData().get(1);
        MetricInformation metricInformation2 = listResponseBody.getData().get(2);
        //推荐此处同时请求的指标数不要超过三个
        List<IndicatorsDetailDataRequestBody> bodyList = new ArrayList<>(3);
        IndicatorsDetailDataRequestBody indicatorsDetailDataRequestBody = IndicatorsDetailDataRequestBody.create(metricInformation1);
//        indicatorsDetailDataRequestBody.setStartTime("20250201");
//        indicatorsDetailDataRequestBody.setEndTime("20250219");
        indicatorsDetailDataRequestBody.setCount("10");
        indicatorsDetailDataRequestBody.setStartIndex("10");
        IndicatorsDetailDataRequestBody indicatorsDetailDataRequestBody1 = IndicatorsDetailDataRequestBody.create(metricInformation2);
        bodyList.add(indicatorsDetailDataRequestBody);
        bodyList.add(indicatorsDetailDataRequestBody1);
        ResponseBody<List<IndicatorInfo>> responseBody = httpRequestClient.requestAgriculturalIndicatorsDetailData(bodyList);
        System.out.println();
    }

    /**
     * 能源系指标
     */
    public void energyIndicatorsInfo() {
        ResponseBody<List<MetricInformation>> listResponseBody = httpRequestClient.requestEnergyIndicatorsInfo(CategoryCommon.EnergyCategoryEnum.ALL);

        MetricInformation metricInformation1 = listResponseBody.getData().get(1);
        MetricInformation metricInformation2 = listResponseBody.getData().get(2);
        //推荐此处同时请求的指标数不要超过三个
        List<IndicatorsDetailDataRequestBody> bodyList = new ArrayList<>(3);
        IndicatorsDetailDataRequestBody indicatorsDetailDataRequestBody = IndicatorsDetailDataRequestBody.create(metricInformation1);
        //        indicatorsDetailDataRequestBody.setStartTime("20250201");
        //        indicatorsDetailDataRequestBody.setEndTime("20250219");
        indicatorsDetailDataRequestBody.setCount("10");
        indicatorsDetailDataRequestBody.setStartIndex("10");
        IndicatorsDetailDataRequestBody indicatorsDetailDataRequestBody1 = IndicatorsDetailDataRequestBody.create(metricInformation2);
        bodyList.add(indicatorsDetailDataRequestBody);
        bodyList.add(indicatorsDetailDataRequestBody1);
        ResponseBody<List<IndicatorInfo>> responseBody = httpRequestClient.requestEnergyIndicatorsDetailData(bodyList);
        System.out.println();
    }

    /**
     * 黑色系指标
     */
    public void blackIndicatorsInfo() {
        ResponseBody<List<MetricInformation>> listResponseBody = httpRequestClient.requestBlackIndicatorsInfo(CategoryCommon.BlackCategoryEnum.ALL);

        MetricInformation metricInformation1 = listResponseBody.getData().get(1);
        MetricInformation metricInformation2 = listResponseBody.getData().get(2);
        //推荐此处同时请求的指标数不要超过三个
        List<IndicatorsDetailDataRequestBody> bodyList = new ArrayList<>(3);
        IndicatorsDetailDataRequestBody indicatorsDetailDataRequestBody = IndicatorsDetailDataRequestBody.create(metricInformation1);
        //        indicatorsDetailDataRequestBody.setStartTime("20250201");
        //        indicatorsDetailDataRequestBody.setEndTime("20250219");
        indicatorsDetailDataRequestBody.setCount("10");
        indicatorsDetailDataRequestBody.setStartIndex("10");
        IndicatorsDetailDataRequestBody indicatorsDetailDataRequestBody1 = IndicatorsDetailDataRequestBody.create(metricInformation2);
        bodyList.add(indicatorsDetailDataRequestBody);
        bodyList.add(indicatorsDetailDataRequestBody1);
        ResponseBody<List<IndicatorInfo>> responseBody = httpRequestClient.requestBlackIndicatorsDetailData(bodyList);
        System.out.println();
    }

    /**
     * 化工系指标
     */
    public void chemicalIndicatorsInfo() {
        ResponseBody<List<MetricInformation>> listResponseBody = httpRequestClient.requestChemicalIndicatorsInfo(CategoryCommon.ChemicalCategoryEnum.ALL);

        MetricInformation metricInformation1 = listResponseBody.getData().get(1);
        MetricInformation metricInformation2 = listResponseBody.getData().get(2);
        //推荐此处同时请求的指标数不要超过三个
        List<IndicatorsDetailDataRequestBody> bodyList = new ArrayList<>(3);
        IndicatorsDetailDataRequestBody indicatorsDetailDataRequestBody = IndicatorsDetailDataRequestBody.create(metricInformation1);
        //        indicatorsDetailDataRequestBody.setStartTime("20250201");
        //        indicatorsDetailDataRequestBody.setEndTime("20250219");
        indicatorsDetailDataRequestBody.setCount("10");
        indicatorsDetailDataRequestBody.setStartIndex("10");
        IndicatorsDetailDataRequestBody indicatorsDetailDataRequestBody1 = IndicatorsDetailDataRequestBody.create(metricInformation2);
        bodyList.add(indicatorsDetailDataRequestBody);
        bodyList.add(indicatorsDetailDataRequestBody1);
        ResponseBody<List<IndicatorInfo>> responseBody = httpRequestClient.requestChemicalIndicatorsDetailData(bodyList);
        System.out.println();
    }

    /**
     * 行情报价
     */
    public void quotes() {

        //商品行情
        ResponseBody<List<Quote>> listResponseBody1 = httpRequestClient.requestQuotes(CategoryCommon.QuoteTypeEnum.GOODS, Arrays.asList("XAU", "NG"));
        //大宗商品
        ResponseBody<List<Quote>> listResponseBody2 = httpRequestClient.requestQuotes(CategoryCommon.QuoteTypeEnum.COMMODITIES, Arrays.asList("LMENI", "WHEAT"));
        //外汇行情
        ResponseBody<List<Quote>> listResponseBody3 = httpRequestClient.requestQuotes(CategoryCommon.QuoteTypeEnum.FOREX, Arrays.asList("USD", "AUDUSD"));
        //全球指数
        ResponseBody<List<Quote>> listResponseBody4 = httpRequestClient.requestQuotes(CategoryCommon.QuoteTypeEnum.GLOBAL_INDEX, Arrays.asList("DJI", "TBI"));
        System.out.println();
        //具体分类中的品种code请查看官方文档,或者联系客服获取
    }

    /**
     * 特色数据
     */
    public void featuredData() {
        //黄金etf
        ResponseBody<DataList<EtfValue>> goldEtf = httpRequestClient.requestEtf(CategoryCommon.EtfTypeEnum.GOLD_ETF);
        //白银etf
        ResponseBody<DataList<EtfValue>> sliverEtf = httpRequestClient.requestEtf(CategoryCommon.EtfTypeEnum.SILVER_ETF);
        //实物金价
        ResponseBody<List<PhysicalGoldPrice>> physicalGoldPrice = httpRequestClient.requestPhysicalGoldPrice();
        //递延方向
        ResponseBody<List<DeferredDirection>> deferredDirection = httpRequestClient.requestDeferredDirection();
        System.out.println("123");
    }

    /**
     * 沪深股票
     */
    public void stock() {
        //股票列表
        ResponseBody<List<StockQuoteInfo>> stockInfoList = httpRequestClient.requestStockList("5", "5");
        //股票十大股东
        ResponseBody<List<ShareholderDataList>> top10Shareholders = httpRequestClient.requestTop10Shareholders(stockInfoList.getData().get(0).getCode());
        //股票十大流通股东
        ResponseBody<List<ShareholderDataList>> outstandingShareholders = httpRequestClient.requestTop10OutstandingShareholders(stockInfoList.getData().get(0).getCode());
        List<String> codeList = stockInfoList.getData().stream().map(StockQuoteInfo::getCode).collect(Collectors.toList());
        //股票每日指标
        ResponseBody<List<DailyMetrics>> dailyMetrics = httpRequestClient.requestStockDailyMetrics(codeList);
        //股票日线行情
        ResponseBody<List<StockQuote>> dailyQuotes = httpRequestClient.requestStockDailyQuotes(codeList);
        //股票财务指标
        ResponseBody<List<FinancialMetrics>> financialMetrics = httpRequestClient.requestStockFinancialMetrics(stockInfoList.getData().get(0).getCode());
        //股票利润表
        ResponseBody<List<IncomeStatement>> incomeStatement = httpRequestClient.requestStockIncomeStatement(stockInfoList.getData().get(0).getCode());
        //股票资产负债表
        ResponseBody<List<BalanceSheet>> StockBalanceSheet = httpRequestClient.requestStockBalanceSheet(stockInfoList.getData().get(0).getCode());
        //股票现金流量表
        ResponseBody<List<CashFlowStatement>> StockCashFlowStatement = httpRequestClient.requestStockCashFlowStatement(stockInfoList.getData().get(0).getCode());
        System.out.println();
    }

    /**
     * 资讯数据
     */
    public void NewsList() {
        //原油资讯
        ResponseBody<Page<News>> oilNewsList = httpRequestClient.requestNewsList(CategoryCommon.NewsTypeEnum.CRUDE_OIL_INFORMATION, "1", "10");
        //股票资讯
        ResponseBody<Page<News>> StockNewsList = httpRequestClient.requestNewsList(CategoryCommon.NewsTypeEnum.STOCK_INFORMATION, "1", "10");
        //贵金属资讯
        ResponseBody<Page<News>> preciousMetalsNewsList = httpRequestClient.requestNewsList(CategoryCommon.NewsTypeEnum.PRECIOUS_METALS_INFORMATION, "1", "10");
        //期货资讯
        ResponseBody<Page<News>> futuresNewsList = httpRequestClient.requestNewsList(CategoryCommon.NewsTypeEnum.FUTURES_INFORMATION, "1", "10");
        //带日历的综合快讯
        ResponseBody<Page<News>> pageResponseBody = httpRequestClient.requestNewsList(CategoryCommon.NewsTypeEnum.NCL, "1", "10");
        //分类下的快讯删除修改记录
        ResponseBody<Page<NewChangeLog>> listResponseBody = httpRequestClient.requestNewChangeList(1, CategoryCommon.NewsTypeEnum.PRECIOUS_METALS_INFORMATION, CategoryCommon.NewsTypeEnum.NCL);
        System.out.println();
    }

    /**
     * 黄金要闻
     */
    public void GoldNews() {
        ResponseBody<List<GoldNews>> listResponseBody = httpRequestClient.requestGoldNewsList(1, 10);
        for (GoldNews datum : listResponseBody.getData()) {
            ResponseBody<GoldNewsDetail> goldNewsDetailResponseBody = httpRequestClient.requestGoldNewsDetail(datum.getId());
        }

        System.out.println();
    }

    /**
     * 期货合约数据分笔分价查询
     */
    public void futuresDataQuery() {
        /**
         * 交易所合约交割信息查询
         */
        ResponseBody<List<DeliveryConfig>> responseBody = httpRequestClient.requestDeliveryConfig(ExchangeCodeEnum.ZZSP);

        String code = responseBody.getData().get(70).getCode();
        //分价查询
        ResponseBody<QuoteVol> stringResponseBody = httpRequestClient.requestQuoteVol(code);
        //分笔首页查询
        ResponseBody<List<RealTimeQuote>> stringResponseBody1 = httpRequestClient.requestRealTimeQuote(code, null, 10);
        //分笔第二页查询  id 传上一页第一条数据的id(根据timestamp判断, timestamp 最小的那条id)
        ResponseBody<List<RealTimeQuote>> listResponseBody = null;
        if (!stringResponseBody1.getData().isEmpty()) {
            listResponseBody = httpRequestClient.requestRealTimeQuote(code, stringResponseBody1.getData().get(0).getId(), 10);
        }
        String futuresKline = httpRequestClient.requestFuturesKline(code, KlineCycleEnum.DAY, 20, null);
        ResponseBody<List<Quote>> quoteList = httpRequestClient.requestFuturesQuotes(Collections.singletonList(code));
        System.out.println();
    }

    /**
     * 财经事件.经济数据.节假日
     */
    public void calendarQuery() {
        String eventData = httpRequestClient.requestCalendarData(LocalDate.now(),CategoryCommon.CalendarTypeEnum.ECONOMIC, CategoryCommon.CalendarTypeEnum.EVENT, CategoryCommon.CalendarTypeEnum.HOLIDAY);

        System.out.println();
    }
}
